/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.gui.mvc.controller.testbed;

import org.testis.gui.mvc.controller.AbstractController;
import org.testis.gui.mvc.controller.TestBedControllerList;
import org.testis.gui.mvc.model.testbed.DeviceFirmwareModel;
import org.testis.gui.mvc.utilities.ParameterTableModel;
import org.testis.gui.mvc.view.testbed.DeviceFirmwareView;
import org.testis.orm.testbed.refined.DeviceDefinitionRefined;
import org.testis.orm.testbed.refined.DeviceFirmwareRefined;
import org.testis.orm.testbed.refined.DynamicMapRefined;
import org.testis.orm.testbed.refined.TestBedItem;
import org.testis.orm.testbed.datahandlers.DeviceFirmwareDataHandler;
import org.testis.orm.testbed.datahandlers.DynamicMapDataHandler;
import org.testis.orm.testbed.refined.DeviceFirmwareFactory;
import org.testis.utils.tree.TbedMutableTreeNode;
import java.util.HashMap;

import org.testis.orm.classes.T_dmap_objects;
import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.utils.middleware.DynamicMap;

/**
 *
 * @author selcuk
 */
public class DeviceFirmwareController extends AbstractController {

    public static final String VISIBLE_PROPERTY = "Visibility";
    public static final String ERROR_PROPERTY = "LastError";
    public static final String FIRMWARE_REFINED_PROPERTY = "FirmwareRefined";
    public static final String FIRMWARE_ID_PROPERTY = "FirmwareId";
    public static final String NAME_PROPERTY = "Name";
    public static final String DESCRIPTION_PROPERTY = "Description";
    public static final String DEVICE_ID_PROPERTY = "DeviceId";
    public static final String MAP_ID_PROPERTY = "DeviceMapId";
    private TbedMutableTreeNode m_selectedNode = null;

    public DeviceFirmwareController() {
        super();
    }

    private void initialize(boolean editMode) {
        DeviceFirmwareModel model = new DeviceFirmwareModel();
        DeviceFirmwareView view = new DeviceFirmwareView(
                TestBedControllerList.newInstance().getParentFrame(),
                true, editMode);
        super.addModel(model);
        super.addView(view);
        view.pack();
    }

    public void operationCloseWindow() {
        super.cleanAll();
    }

    public void operationEditFirmware(TbedMutableTreeNode node) {
        if (node.getUserObject() instanceof DeviceFirmwareRefined) {
            m_selectedNode = node;
            DeviceFirmwareRefined deviceFirmware = getSelectedFirmware();
            if (deviceFirmware == null) {
                System.out.println(
                        "Not found design part for DeviceFirmwareRefined");
                return;
            }
            initialize(true);
            setModelProperty(FIRMWARE_REFINED_PROPERTY, deviceFirmware);
            setModelProperty(FIRMWARE_ID_PROPERTY, deviceFirmware.getId());
            setModelProperty(NAME_PROPERTY, deviceFirmware.getName());
            setModelProperty(DESCRIPTION_PROPERTY,
                    deviceFirmware.getDescription());
            setModelProperty(DEVICE_ID_PROPERTY,
                    deviceFirmware.getDeviceDefinition().getId());
            DynamicMapRefined dynamicMap = deviceFirmware.getDynamicMap();
            if (dynamicMap != null) {
                setModelProperty(MAP_ID_PROPERTY, dynamicMap.getId());
            }
            setModelProperty(VISIBLE_PROPERTY, true);
        }
    }

    public void operationEditSelectedFirmware() {
        TbedMutableTreeNode node = TestBedControllerList.newInstance().getTbedTreeController().getSelectedTreeNode();
        operationEditFirmware(node);
    }

    public void operationNewFirmware() {
        initialize(false);
        TestBedItem testBedItem =
                TestBedControllerList.newInstance().getTbedTreeController().getSelectedTestBedItem();
        if (testBedItem != null && testBedItem instanceof DeviceDefinitionRefined) {
            DeviceDefinitionRefined device = (DeviceDefinitionRefined) testBedItem;
            setModelProperty(DEVICE_ID_PROPERTY, device.getId());
        }
        setModelProperty(VISIBLE_PROPERTY, true);
    }

    public void operationAddFirmware(String name, int deviceId, int dmapDesigntreeId, String description) {
        DeviceFirmwareRefined firmware = DeviceFirmwareFactory.newInstance().createDeviceFirmware(
                name, deviceId,
                dmapDesigntreeId, description);
        if (firmware != null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationAddTestBedItem(
                    firmware);
        } else {
            setModelProperty(ERROR_PROPERTY, "Unable to add firmware record");
        }
    }

    public void operationAddFirmware(String name, int deviceId, String newDynamicMapName, String description) {
        int dmapDesigntreeId = operationAddDynamicMap(newDynamicMapName);
        if (dmapDesigntreeId != 0) {
            operationAddFirmware(name, deviceId, dmapDesigntreeId, description);
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to add DynamicMap record for new firmware");
        }
    }

    public void operationSaveFirmware(DeviceFirmwareRefined refined, HashMap values) {
        if (refined == null) {
            TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                    "Unable to edit firmware, firmwareRefined is null");
            return;
        }

        Integer id = (Integer) values.get("id");
        String name = (String) values.get("name");
        Integer deviceId = (Integer) values.get("deviceId");
        String description = (String) values.get("description");
        Integer dmapDesigntreeId;
        if (values.containsKey("newDynamicMapName")) {
            String newDynamicMapName = (String) values.get(
                    "newDynamicMapName");
            dmapDesigntreeId = operationAddDynamicMap(newDynamicMapName);
            if (dmapDesigntreeId == 0) {
                setModelProperty(ERROR_PROPERTY,
                        "Unable to add DynamicMap record for firmware");
                return;
            }
        } else {
            dmapDesigntreeId = (Integer) values.get("dmapDesigntreeId");
        }

        boolean move = (refined.getDeviceDefinition().getId() != deviceId);
        if (DeviceFirmwareDataHandler.newInstance().saveFirmware(
                id,
                name, deviceId,
                dmapDesigntreeId, description)) {
            if (move) { //move node from one to another
                TestBedControllerList.newInstance().getTbedTreeController().operationMoveSelectedTestBedItem(
                        deviceId);
            }
        } else {
            setModelProperty(ERROR_PROPERTY,
                    "Unable to edit firmware record");
        }
    }

    public int operationAddDynamicMap(String name) {
        int dmapDesigntreeId = 0;
        ParameterTableModel tableModel = new ParameterTableModel();
        tableModel.addItem("IpAddressTxtAbstractId", "");
        tableModel.addItem("IpAddressBtnAbstractId", "");
        tableModel.addItem("Name", "");
        tableModel.addItem("FactorySettingsIp", "");
        tableModel.addItem("LoginPageId", "");
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("name", name);
        params.put("parameters", tableModel);
        params.put("type", "UIDeviceMapContainer");
        DynamicMapDesignTreeNode addedNode = DynamicMap.newInstance().operationAddNewObject(
                params, null, 0);
        if (addedNode != null) {
            dmapDesigntreeId = addedNode.getId();
            T_dmap_objects dmapObject = addedNode.getReference().getObject();
            DynamicMapDataHandler.newInstance().putFlyweight(dmapObject.getId(),
                    dmapObject);
        }
        return dmapDesigntreeId;
    }

    public void operationDeleteFirmware(DeviceFirmwareRefined firmwareRefined) {
        if (firmwareRefined != null) {
            int firmwareId = firmwareRefined.getId();
            DeviceDefinitionRefined deviceRefined = firmwareRefined.getDeviceDefinition();
            if (DeviceFirmwareDataHandler.newInstance().deleteFirmware(
                    firmwareId)) {
                deviceRefined.removeFirmware(firmwareId);
                TestBedControllerList.newInstance().getTbedTreeController().operationRemoveSelectedTestBedItem();
            } else {
                TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                        "Unable to remove firmware record");
            }
        } else {
            TestBedControllerList.newInstance().getTbedTreeController().operationSetErrorProperty(
                    "Unable to remove firmware, firmwareRefined is null");
        }
    }

    public DeviceFirmwareRefined getSelectedFirmware() {
        DeviceFirmwareRefined result = null;
        Object obj = m_selectedNode.getUserObject();
        if (obj != null && obj instanceof DeviceFirmwareRefined) {
            result = (DeviceFirmwareRefined) obj;
        }
        return result;
    }
}
